﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PSI.Helper {
    /// <summary>
    /// 自定义JsonResult，用Json.NET来序列化对象
    /// </summary>
    public class CustomJsonResult : System.Web.Mvc.JsonResult {
        public override void ExecuteResult(System.Web.Mvc.ControllerContext context) {
            if (context == null) {
                throw new ArgumentNullException("context");
            }

            var response = context.HttpContext.Response;
            if (!String.IsNullOrEmpty(ContentType)) {
                response.ContentType = ContentType;
            } else {
                response.ContentType = "application/json";
            }

            if (ContentEncoding != null) {
                response.ContentEncoding = ContentEncoding;
            }

            if (Data != null) {
                response.Write(JsonConvert.SerializeObject(Data,
                    new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    }));
            }
        }
    }
}